package com.ph.irp.stock.action;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import com.opensymphony.xwork2.ActionSupport;
import com.ph.irp.base.currency.service.CurrencyService;
import com.ph.irp.dto.PageDTO;
import com.ph.irp.dto.StockOrderDTO;
import com.ph.irp.model.PStockOrder;
import com.ph.irp.model.PStockOrderdetail;
import com.ph.irp.stock.service.StockOrderService;
import com.ph.util.DateUtil;
import com.ph.util.pagination.PageUtil;

public class StockOrderAction extends ActionSupport {
	private int i = 1 ;
	//采购订单model
	private PStockOrder stockOrderModel ;
	//采购订单service
	private StockOrderService stockOrderService ;
	
	//采购订单dto list
	private List<StockOrderDTO> stockOrders ;
	
	//临时采购订单list
	private List<StockOrderDTO> tempStockOrders ;
	
	//分页信息
	private String cpage;
	
	private PageDTO page;
	
	//采购明细物料id数组
	private String[] materialId ;
	
	//采购明细计量单位id数组
	private String[] measureUnitId ;
	
	//采购明细计物料数量数组
	private String[] cost ;	
	
	//采购明细单价数组
	private String[] unitPrice ;
	
	//含税单价
	private String[] totalUnitPrice ;
	
	//money金额
	private String[] money;
	
	//物料折扣率数组
	private String[] discount ;
	//实际含税单价数组
	private String[] realTotalUnitPrice ;
	
	//折扣额
	private String[] discountPrice ;
	
	//税率数组
	private String[] dutyFrank ;
	
	//税额数组
	private String[] dutyPrice ;
	
	//价格合计
	private String[] totalMoney ;
	
	//交货日期数组
	private String[] deliveryDate ;
	
	//备注数组
	private String[] remark ;
	
	//其实日期
	private String startDate ;
	//结束日期
	private String endDate ;
		
	public String execute()throws Exception{
		
		try {
			return showStockOrders();
		} catch (Throwable e) {
			// TODO Auto-generated catch block
			throw new Exception(e.getMessage());
		}				
	
	}

	public String showStockOrders()throws Throwable{
		System.out.println("here is stockOrderaction   " +i++);
		
		tempStockOrders = stockOrderService.getStockOrdersByCondition(stockOrderModel, startDate, endDate);
		System.out.println("here is stockOrderaction   " +i++);
		page = PageUtil.generatePageDTO(tempStockOrders,
				"showStockOrders.action", cpage);
		stockOrders = page.getResList() ;
		return "success" ;
	}
	
	public String addInputStockOrder()throws Throwable{	
		
		stockOrderModel = new PStockOrder();
		//为新增采购订单设置默认的时间
		Date currentDate = new Date();
		String stringDate =  DateUtil.date2String(currentDate,"yyyy-MM-dd");
		//设置采购日期和结算日期
		stockOrderModel.setBuiltTime(stringDate) ;
		stockOrderModel.setBalanceDate(stringDate) ;
		
		return "success";
	}
	
	public String addStockOrder()throws Throwable{
		
		List<PStockOrderdetail> stockOrderDetails = new ArrayList<PStockOrderdetail>();
		//检查是否有明细
		if(materialId!=null){
			for(int i=0;i<materialId.length;i++){
				//将明细封装到PStockOrderdetail中
				PStockOrderdetail pStockOrderdetail = new PStockOrderdetail();
				pStockOrderdetail.setMaterialId(Integer.valueOf(materialId[i])) ;
				pStockOrderdetail.setMeasureunitId(Integer.valueOf(measureUnitId[i])) ;
				pStockOrderdetail.setStockAmount(Float.valueOf(cost[i])) ;
				pStockOrderdetail.setUnitPrice(Float.valueOf(unitPrice[i])) ;
				pStockOrderdetail.setTotalUnitPrice(Float.valueOf(totalUnitPrice[i])) ;
				pStockOrderdetail.setMoney(Float.valueOf(money[i])) ;
				pStockOrderdetail.setDiscount(Float.valueOf(discount[i])) ;
				pStockOrderdetail.setRealTotalUnitPrice(Float.valueOf(realTotalUnitPrice[i])) ;
				pStockOrderdetail.setDiscount(Float.valueOf(discountPrice[i])) ;
				pStockOrderdetail.setDutyFrank(Float.valueOf(dutyFrank[i])) ;
				pStockOrderdetail.setDutyPrice(Float.valueOf(dutyPrice[i])) ;
				pStockOrderdetail.setTotalMoney(Float.valueOf(totalMoney[i])) ;
				pStockOrderdetail.setDeliveryDate(deliveryDate[i]);
				pStockOrderdetail.setRemark(remark[i]) ;
				stockOrderDetails.add(pStockOrderdetail) ;
			}
		}
		else
			return "error" ;
		try {
			//调用service方法保存采购订单及其明细
			stockOrderService.addStockOrder(stockOrderModel,stockOrderDetails ) ;
		} catch (Throwable e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		return "success" ;
	}
	
	/*getters and setters for instance variables*/
	public PStockOrder getStockOrderModel() {
		return stockOrderModel;
	}

	public void setStockOrderModel(PStockOrder stockOrderModel) {
		this.stockOrderModel = stockOrderModel;
	}

	public StockOrderService getStockOrderService() {
		return stockOrderService;
	}

	public void setStockOrderService(StockOrderService stockOrderService) {
		this.stockOrderService = stockOrderService;
	}

	public String[] getMaterialId() {
		return materialId;
	}

	public void setMaterialId(String[] materialId) {
		this.materialId = materialId;
	}

	public String[] getMeasureUnitId() {
		return measureUnitId;
	}

	public void setMeasureUnitId(String[] measureUnitId) {
		this.measureUnitId = measureUnitId;
	}

	public String[] getCost() {
		return cost;
	}

	public void setCost(String[] cost) {
		this.cost = cost;
	}

	public String[] getUnitPrice() {
		return unitPrice;
	}

	public void setUnitPrice(String[] unitPrice) {
		this.unitPrice = unitPrice;
	}

	public String[] getTotalUnitPrice() {
		return totalUnitPrice;
	}

	public void setTotalUnitPrice(String[] totalUnitPrice) {
		this.totalUnitPrice = totalUnitPrice;
	}

	public String[] getMoney() {
		return money;
	}

	public void setMoney(String[] money) {
		this.money = money;
	}

	public String[] getDiscount() {
		return discount;
	}

	public void setDiscount(String[] discount) {
		this.discount = discount;
	}

	public String[] getRealTotalUnitPrice() {
		return realTotalUnitPrice;
	}

	public void setRealTotalUnitPrice(String[] realTotalUnitPrice) {
		this.realTotalUnitPrice = realTotalUnitPrice;
	}

	public String[] getDiscountPrice() {
		return discountPrice;
	}

	public void setDiscountPrice(String[] discountPrice) {
		this.discountPrice = discountPrice;
	}

	public String[] getDutyFrank() {
		return dutyFrank;
	}

	public void setDutyFrank(String[] dutyFrank) {
		this.dutyFrank = dutyFrank;
	}

	public String[] getDutyPrice() {
		return dutyPrice;
	}

	public void setDutyPrice(String[] dutyPrice) {
		this.dutyPrice = dutyPrice;
	}

	public String[] getTotalMoney() {
		return totalMoney;
	}

	public void setTotalMoney(String[] totalMoney) {
		this.totalMoney = totalMoney;
	}

	public String[] getDeliveryDate() {
		return deliveryDate;
	}

	public void setDeliveryDate(String[] deliveryDate) {
		this.deliveryDate = deliveryDate;
	}

	public String[] getRemark() {
		return remark;
	}

	public void setRemark(String[] remark) {
		this.remark = remark;
	}

	public List<StockOrderDTO> getStockOrders() {
		return stockOrders;
	}

	public void setStockOrders(List<StockOrderDTO> stockOrders) {
		this.stockOrders = stockOrders;
	}

	public List<StockOrderDTO> getTempStockOrders() {
		return tempStockOrders;
	}

	public void setTempStockOrders(List<StockOrderDTO> tempStockOrders) {
		this.tempStockOrders = tempStockOrders;
	}

	public String getCpage() {
		return cpage;
	}

	public void setCpage(String cpage) {
		this.cpage = cpage;
	}

	public PageDTO getPage() {
		return page;
	}

	public void setPage(PageDTO page) {
		this.page = page;
	}

	public String getStartDate() {
		return startDate;
	}

	public void setStartDate(String startDate) {
		this.startDate = startDate;
	}

	public String getEndDate() {
		return endDate;
	}

	public void setEndDate(String endDate) {
		this.endDate = endDate;
	}		
}
